<?php
/*
 * @Author: your name
 * @Date: 2020-12-23 12:01:16
 * @LastEditTime: 2020-12-28 10:13:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/common/service/module_manage/hgshield/NetworkAttack.php
 */
namespace app\common\service;


use function Couchbase\fastlzCompress;
use think\Db;

class NetworkAttack
{
    /**
     * @name 列表
     * @param $params
     * @return array
     * @author wx
     * @date 2019/11/14 14:42
     */
    public function list($params)
    {
        $field = 'id, host_id, description, src_ip, dest_ip, domain_name, url,
          atime, type, level, handle, rule_id, event_id, allow_ip_rule_id, ctime';

        $db = Db::name('network_attack');

        isset($params['host_id']) && !empty($params['host_id']) ? $db->whereIn('host_id', $params['host_id']): '';
        !isset($params['description']) || empty($params['description']) ? : $db->whereLike('description', '%'. $params['description'] . '%');
        !isset($params['domain_name']) || empty($params['domain_name']) ? : $db->whereLike('domain_name', '%'. $params['domain_name'] .'%');
        !isset($params['src_ip']) || empty($params['src_ip']) ? : $db->whereLike('src_ip', '%'. $params['src_ip'] .'%');
        !isset($params['dest_ip']) || empty($params['dest_ip']) ? : $db->whereLike('dest_ip', '%'. $params['dest_ip'] .'%');
        !isset($params['url']) || empty($params['url']) ? : $db->whereLike('url', '%'. $params['url'] .'%');
        if (isset($params['atime']) && !empty($params['atime']))
        {
            $stime = strtotime($params['atime']);
            $etime = $stime + 86400;
            $db->whereBetween('atime', [$stime,$etime]);
        }
        !isset($params['type']) || !is_numeric($params['type']) ? : $db->where('type', $params['type']);
        !isset($params['level']) || !is_numeric($params['level']) ? : $db->where('level', $params['level']);

        //处理结果
        if (isset($params['handle']) && is_numeric($params['handle'])) {
            if(in_array($params['handle'], [1, 32])){
                $db->where('handle', $params['handle']);
            }else{
                $db->whereNotIn('handle', [1,32]);
            }
        }


        $return = $db->field('id')->order('atime desc')->paginate()->toArray();
        $id_a = array_column($return['data'], 'id');
        $return['data'] = Db::name('network_attack')->field($field)->whereIn('id', $id_a)->orderField('id', $id_a)->select();

        return $return;
    }

    /**
     * @name 查询数据
     * @param $condition
     * @param string $field
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/11/15 14:40
     */
    public function findData($condition, $field = '*')
    {
        $info = Db::name('network_attack')
            ->field($field)
            ->where($condition)->find();

        return $info;
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/11/15 14:41
     */
    public function modify($condition, $updateData)
    {
        return Db::name('network_attack')->where($condition)->update($updateData);
    }
}